package com.ltmonitor.service;

import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.thread.BasicServiceThread;
import com.ltmonitor.thread.BtdTimeThread;
import com.ltmonitor.thread.CheckGpsInsertServiceThread;
import com.ltmonitor.thread.GetChCardPicServiceThread;
import com.ltmonitor.thread.GetDisContinuityServiceThread;
import com.ltmonitor.thread.TestServiceThread;
import com.ltmonitor.thread.ThreadManageService;

import java.io.InputStream;
import java.lang.Thread;
import java.util.Properties;



/**
 * 在系统准备完成后需要启动的服务
 * @author shinion
 *
 */

public class StartServiceAfterReady extends HttpServlet {
	
	private Logger threadLog = Logger.getLogger("thread");
	
	
	private BtdTimeThread timerThread  = null;
	
	//定义线程管理器
	private ThreadManageService threadManager = null;
	
	//首先执行
	public void init() {
		try {
			
//			System.out.println("++++++++++++++++++++++++++++++++");
//			System.out.println("++++++++++++++++++++++++++++++++");
//			System.out.println("开始加载");
			
			//1.加载定时执行线程
			if(timerThread == null) {
				timerThread = new BtdTimeThread();
				timerThread.start();
				System.out.println("加载定时器线程完成!");
			}
			
			//初始化线程管理器
			initManager();
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	//初始化线程管理器
	private void initManager() {
		
		/*
		 * 线程管理器定义5秒检查一次，所管理的线程单次循环时间不得超过15秒，否则进程校验可能会出问题
		 * */
		if(threadManager == null) {
			Properties pro1 = getPro();
			int switchValue = 0;
			if(pro1.containsKey("switch")) {
				try {
					switchValue = Integer.valueOf(pro1.getProperty("switch"));					
				} catch (Exception e) {
					// TODO: handle exception
				}
				if(switchValue == 1) {
					threadLog.info("线程管理器开关值为" + switchValue  + "  ，开始初始化线程管理器............");
					threadManager = new ThreadManageService("线程管理器", 1000 * 5);//定义线程管理器，5秒检查一次
					threadManager.start();								
				}else {
					threadLog.info("线程管理器开关值为" + switchValue  + "  ，不加载线程管理器！");
				}
			}
		}
	}
	
	//销毁执行
	public void destroy() {
		try {
			System.out.println("++++++++++++++++++++++++++++++++");
			System.out.println("++++++++++++++++++++++++++++++++");
			System.out.println("开始销毁");
			//停止线程
			if(timerThread != null) {
				timerThread.setThreadFlg(false);
				timerThread = null;
			}
			//停止线程管理器
			if(threadManager != null) {
				threadManager.stopAll();
				threadManager.stopRun();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 读取线程开关配置文件并返回对象
	 * @return
	 */
	private  Properties getPro() {
		Properties properties = new Properties();
		InputStream is =  StartServiceAfterReady.class.getClassLoader().getResourceAsStream("serviceThreadConf.properties");
		try {
			properties.load(is);
		} catch (Exception e) {
			// TODO: handle exception
			String msg = "获取服务线程配置文件失败 ：" + e.getMessage();
			System.out.println(msg);
		}
		return properties;
	}
	

}
